#coding=utf-8
import traceback
from mysql_oper import MysqlOper

class UrlManager2(object):
    def __init__(self):
        self.mysql_oper = MysqlOper()
    
    def add_new_url(self,url):
        if url is None:
            return
        try:
            new_sql = 'select * from new_url where url = %s'
            new_cnt = self.mysql_oper.exec_query(new_sql,[url])
            old_sql = 'select * from old_url where url = %s'
            old_cnt = self.mysql_oper.exec_query(old_sql,[url])
            if (new_cnt is None and old_cnt is None):
                sql = "insert into new_url(url) values ('%s')"% url
                self.mysql_oper.exec_dml(sql,[])
        except Exception as e:
                print 'traceback.format_exc():\n%s' % traceback.format_exc()
    
    def add_new_urls(self,urls):
        if urls is None or len(urls) == 0:
            return
        for url in urls:
            self.add_new_url(url)
    
    def has_new_url(self):
        sql = 'select * from new_url limit 1'
        cnt = self.mysql_oper.exec_query(sql,[])
        return cnt is not None

    
    def get_new_url(self):
        sql = 'select * from new_url limit 1'
        new_url = self.mysql_oper.exec_query(sql,())
        if new_url is not None:
            print new_url
            sql2 = "insert into old_url(url) values ('%s')"% new_url
            self.mysql_oper.exec_dml(sql2,[])
            
            sql3 = "delete from new_url where url = '%s'"% new_url
            self.mysql_oper.exec_dml(sql3,[])
            return new_url
        
    
    
    
    
    
    
    
    
    



